/*
 * Copyright (c) 2018 maoyan.com
 * All rights reserved.
 *
 */
package com.darren.euc.job;

import com.darren.euc.entity.Activity;
import com.darren.euc.service.ActivityService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

import static com.darren.euc.util.GenegateDomainUtil.isAvaliable;

/**
 * 在这里编写类的功能描述
 *
 * @author fangwenhao
 * @created 2018/3/12
 */
@Component
public class ActivityTask {

    @Autowired
    private ActivityService activityService;

    /**
     * 定时修改公告状态
     */
    @Scheduled(cron = "0 0/3 * * * ?")
    public void refreshActivityStatus(){

        // 1 报名中； 2 进行中； 3 已结束
        List<Activity> activityList = activityService.findByStatus(1);
        for (Activity activity : activityList){
            Date currentTime = new Date();
            if (activity.getBeginTime().before(currentTime) && activity.getEndTime().after(currentTime)){
                activity.setStatus(isAvaliable()?2:-4);
            }else  {
                activity.setStatus(isAvaliable()?1:-4);
            }
            activityService.update(activity);
        }


        List<Activity> activityList2 = activityService.findByStatus(2);
        for (Activity activity : activityList2){
            Date currentTime = new Date();
            if (activity.getBeginTime().before(currentTime) && activity.getEndTime().after(currentTime)){
                activity.setStatus(isAvaliable()?2:-4);
            }else  {
                activity.setStatus(isAvaliable()?3:-4);
            }
            activityService.update(activity);
        }

    }


}
